package com.swgk.core.common.utils;

import android.app.Activity;
import android.os.Bundle;

import androidx.core.util.Pair;

import com.alibaba.android.arouter.launcher.ARouter;

import java.io.Serializable;

/**
 * @author: 王彬臣
 * @date: 2021/7/13
 * @Description: 路由跳转工具类
 */
public class ActivityJump {

    @SafeVarargs
    public static void startActivity(String path, Pair<String,Object>...o){
        if (o.length <= 0){
            ARouter.getInstance().build(path).navigation();
        }else {
            Bundle bundle = new Bundle();
            for (Pair<String,Object> o1 : o) {
                if (o1.second instanceof Integer){
                    bundle.putInt(o1.first, (Integer) o1.second);
                    continue;
                } else if (o1.second instanceof Short){
                    bundle.putShort(o1.first, (Short) o1.second);
                    continue;
                } else if (o1.second instanceof Byte){
                    bundle.putByte(o1.first, (Byte) o1.second);
                    continue;
                }else if (o1.second instanceof Long){
                    bundle.putLong(o1.first, (Long) o1.second);
                    continue;
                }else if (o1.second instanceof Float){
                    bundle.putFloat(o1.first, (Float) o1.second);
                    continue;
                }else if (o1.second instanceof Double){
                    bundle.putDouble(o1.first, (Double) o1.second);
                    continue;
                }else if (o1.second instanceof Character){
                    bundle.putChar(o1.first, (Character) o1.second);
                    continue;
                }else if (o1.second instanceof String){
                    bundle.putString(o1.first, (String) o1.second);
                    continue;
                }else if (o1.second instanceof Serializable){
                    bundle.putSerializable(o1.first, (Serializable) o1.second);
                    continue;
                }else {
                    throw new RuntimeException("没有这样的类型");
                }
            }
            ARouter.getInstance().build(path).with(bundle).navigation();
        }
    }

    @SafeVarargs
    public static void startActivityForResult(Activity activity, int requestCode, String path, Pair<String,Object>...o){
        if (o.length <= 0){
            ARouter.getInstance().build(path).navigation(activity,requestCode);
        }else {
            Bundle bundle = new Bundle();
            for (Pair<String,Object> o1 : o) {
                if (o1.second instanceof Integer){
                    bundle.putInt(o1.first, (Integer) o1.second);
                    continue;
                } else if (o1.second instanceof Short){
                    bundle.putShort(o1.first, (Short) o1.second);
                    continue;
                } else if (o1.second instanceof Byte){
                    bundle.putByte(o1.first, (Byte) o1.second);
                    continue;
                }else if (o1.second instanceof Long){
                    bundle.putLong(o1.first, (Long) o1.second);
                    continue;
                }else if (o1.second instanceof Float){
                    bundle.putFloat(o1.first, (Float) o1.second);
                    continue;
                }else if (o1.second instanceof Double){
                    bundle.putDouble(o1.first, (Double) o1.second);
                    continue;
                }else if (o1.second instanceof Character){
                    bundle.putChar(o1.first, (Character) o1.second);
                    continue;
                }else if (o1.second instanceof String){
                    bundle.putString(o1.first, (String) o1.second);
                    continue;
                }else if (o1.second instanceof Serializable){
                    bundle.putSerializable(o1.first, (Serializable) o1.second);
                    continue;
                }else if (o1.second == null){
                    throw new RuntimeException(o1.first+"这个对象空了");
                }else {
                    throw new RuntimeException("没有这样的类型");
                }
            }
            ARouter.getInstance().build(path).with(bundle).navigation(activity,requestCode);
        }
    }
}
